import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import('../views/Home.vue'),
    meta:{
      title:'主页',
      requiresAuth:false
    }
  },
  {
    path: "/blogs",
    name: "Blogs",
    component: () => import('../views/Blogs.vue'),
    meta: {
      title: '博客',
      requiresAuth: false
    }
  },
  {
    path: "/login",
    name: "Login",
    component: () => import('../views/Login.vue'),
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: "/register",
    name: "Register",
    component: () => import('../views/Register'),
    meta: {
      title: '注册',
      requiresAuth: false      
    }
  },
  {
    path: "/forgotpassword",
    name: "ForgotPassword",
    component: () => import('../views/ForgotPassword.vue'),
    meta: {
      title: '忘记密码',
      requiresAuth: false      
    }
  },
  {
    path: "/profile",
    name: "Profile",
    component: () => import('../views/Profile.vue'),
    meta: {
      title: '个人信息',
      requiresAuth: true      
    }
  },
  {
    path: "/create-post",
    name: "CreatePost",
    component: () => import('../views/CreatePost.vue'),
    meta: {
      title: '创建博客',
      requiresAuth: true
    }
  },
  {
    path: "/post-preview",
    name: "BlogPreview",
    component: () => import('../views/BlogPreview.vue'),
    meta: {
      title: '发布预览',
      requiresAuth: true
    }
  },
  {
    path: "/view-blog/:blogid",
    name: "ViewBlog",
    component: () => import('../views/ViewBlog.vue'),
    meta: {
      title: '查看博客',
      requiresAuth: false
    }
  },
  {
    path: "/edit-blog/:blogid",
    name: "EditBlog",
    component: () => import('../views/EditBlog.vue'),
    meta: {
      title: '编辑博客',
      requiresAuth: true
    }
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to,from,next) =>{
  document.body.scrollTop = 0;
  document.documentElement.scrollTop = 0;
  // 兼容IE
  window.scrollTo(0, 0);
  document.title = `${to.meta.title}`
  next();
})

export default router;
